/*
 *	Perlin Production Co. Ltd. September 2012 (c).
 *			clever.c
 *		Color Level Resizer
 */

#include "stdio.h"
#include "fdlib.h"
#include "paramlib.h"
#include "WinGDI.h"
#include "clrspc.h"
 
#define	IP_RED		1	/* red   level */
#define	IP_GREEN	2	/* green   level */
#define	IP_BLUE		3	/* blue   level */
#define IP_LUMO		4	/* luminosity */
#define IP_GREY		5	/* greyscale */

 
#define NUM_IP	 6
INTPARM	params	[NUM_IP];

char	clrname	[0x80];
RGB	rgbMax;
RGB	rgbCLR	[0x100];	/*Vector Color Space converted to true color*/


InitOpt(_ip)
INTPARM	*_ip;
{
 InitParm(_ip++, 'v', VERBMIN, VERBDEF, VERBMAX, "verbosity");
 InitParm(_ip++, 'r', 0 , 255, 255, "red level");
 InitParm(_ip++, 'g', 0 , 255, 255, "green level");
 InitParm(_ip++, 'b', 0 , 255, 255, "blue level");
 InitParm(_ip++, 'l', 0 , 127, 255, "lumo gain");
 InitParm(_ip++, 'w', 0 , 000, 001, "greyscale");
}


 
int main(_argc,_argv)
int	_argc;
char	**_argv;
{
	FILE		*fCLR;
	int		verbose;
	int		isGrey;
	int		i;
	RGB		*rgb;
	RGB*		(*expand)();	/* pointer to color space expansion */
	RGB*		(*scale)();	/* pointer to scaling function */
	
	if (_argc < 2) usage(0);
	
	if (*_argv[1] == '-') trouble("Required parameter missing:","output[.CLR]");
	
	InitOpt(params);
	
	if (_argc > 2) OptParm(_argv, 2, _argc, params, NUM_IP);
	
	verbose	= params[IP_VERBOSE].ip_val;
	
	ExtAdd(clrname , _argv[1], 0x7F, ".CLR");
	
	InitRGB	( &rgbMax
		, params[IP_RED].ip_val
		, params[IP_GREEN].ip_val
		, params[IP_BLUE].ip_val
		);
	rgbMax.rgbReserved = params[IP_LUMO].ip_val;
	isGrey = params[IP_GREY].ip_val;
	
	if (verbose == VERBMAX) PutRGB(&rgbMax, "RGBMAX:");
		
	expand	= isGrey ? CSEBW : CSELin;
	scale	= CSNLin;

	CLR_Gen(rgbCLR, &rgbMax, expand, scale, verbose);
	
	return CLR_Wr(rgbCLR, clrname, verbose);
}	
	

	
usage(_text)
char* _text;
{
 printf("Perlin Production Co. Ltd. September 2012\n");
 printf("*** COLOR LEVER ***, ver. 1.1\n");
 printf("Generates component levels of Vector-06c palette, translated\n");
 printf("from 233 to 888 colorspace. Saves levels to .CLR file.\n");
 printf("\nTo display BMP image on Vector-06c, do the following:\n");
 printf("1. Use IDC utility to convert .BMP image into Vector-06c .BIN format.\n");
 rev_vid();
 printf("2. Use CLR (this) to generate colorspace component levels .CLR file.\n");
 nrm_vid();
 printf("3. Use APG utility to get image palette from .BMP and .CLR files.\n");
 printf("4. Use SHIM image viewer to display .BIN image in .PAL palette.\n");
 
 printf("Usage:\n");
 rev_vid();
 printf("CLR output[.clr] [options]\n");
 nrm_vid();
 printf("-- Optional Parameters:\n");
 printf("[-v[%d]] - verbosity level %d-%d\n"	, VERBDEF, VERBMIN, VERBMAX);
 printf("[-r[%d]] - red   level %d-%d\n"	, 255, 0, 255);
 printf("[-g[%d]] - green level %d-%d\n"	, 255, 0, 255);
 printf("[-b[%d]] - blue  level %d-%d\n"	, 255, 0, 255);
 printf("[-l[%d]] - luminosity gain %d-%d\n"	, 127, 0, 255);
 printf("[-w[%d]] - greyscale output %d-%d\n"	, 0,0,1);
 
  if (_text) trouble(_text, 0);
 else exit(0);
}
